package com.yixin.ystartlibrary.utils;

import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.AlertDialog;

import java.net.URISyntaxException;

/**
 * User: Luozi
 * Date: 2016-09-29
 * Content: 地图导航,调用第三方地图
 */

public class MapNavigationUtils {

    private static final String PACKAGE_GAODE = "com.autonavi.minimap";
    private static final String PACKAGE_BAIDU = "com.baidu.BaiduMap";

    public static void startNavigation(Activity context, String sLat, String sLng, String endAddress, String lat, String lng) {

        if (Utils.isAvailable(context, PACKAGE_GAODE)) {
            //高德地图
            String uri = getGaodeUri(sLat, sLng, endAddress, lat, lng);
            LogUtils.e("高德 : " + uri);
            Intent intent = new Intent(Intent.ACTION_VIEW, android.net.Uri.parse(uri));
            intent.setPackage(PACKAGE_GAODE);
            context.startActivity(intent);

        } else if (Utils.isAvailable(context, PACKAGE_BAIDU)) {
            //百度地图
            String uri = getBaiduUri(sLat, sLng, endAddress, lat, lng);
            LogUtils.e("百度 : " + uri);
            Intent intent = null;
            try {
                intent = Intent.parseUri(uri, Intent.URI_INTENT_SCHEME);
            } catch (URISyntaxException e) {
                e.printStackTrace();
            }
            context.startActivity(intent);
        } else {
            new AlertDialog.Builder(context)
                    .setTitle("提示")
                    .setMessage("导航失败, 请下载百度地图或高德地图才能使用店铺导航")
                    .setPositiveButton("确定", null)
                    .show();
        }
    }

    private static String getBaiduUri(String sLat, String sLng, String endAddress, String lat, String lng) {
        StringBuilder sb = new StringBuilder("intent://map/direction?");
        sb.append("origin=latlng:").append(sLat).append(",").append(sLng).append("|name:当前位置&");
        sb.append("destination=latlng:").append(lat).append(",").append(lng).append("|name:").append(endAddress).append("&");
        sb.append("mode=driving&region=中国&src=lckj|机回收#Intent;scheme=bdapp;package=com.baidu.BaiduMap;end");
        return sb.toString();
    }

    private static String getGaodeUri(String sLat, String sLng, String endAddress, String lat, String lng) {
        StringBuilder sb = new StringBuilder("androidamap://route?");
        sb.append("sourceApplication=机回收&");
        sb.append("slat=").append(sLat).append("&").append("slon=").append(sLng).append("&sname=当前位置&");
        sb.append("dlat=").append(lat).append("&").append("dlon=").append(lng).append("&").append("dname=").append(endAddress).append("&");
        sb.append("dev=0&m=2&t=2");
        return sb.toString();
    }

}
